package com.android.advertising;

import java.util.ArrayList;
import java.util.Collections;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ArticleDatabase {
	private ArticleDatabaseHelper databaseHelper;
	private String[] allColumns = { Constants.ARTICLES_ID,
			Constants.ARTICLES_SERVER_ID, Constants.ARTICLES_TITLE,
			Constants.ARTICLES_TIME, Constants.ARTICLES_LINK,
			Constants.ARTICLES_THUMBLINK, Constants.ARTICLES_ISREAD,
			Constants.ARTICLES_ISACTIVE };
	private SQLiteDatabase readableDatabase, writableDatabase;
	private static final String TAG = "Database";
	private static ArticleDatabase instance = null;

	public static ArticleDatabase getInstance(Context context) {
		if (instance == null)
			instance = new ArticleDatabase(context);
		return instance;
	}

	private ArticleDatabase(Context context) {
		databaseHelper = new ArticleDatabaseHelper(context,
				Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
		writableDatabase = databaseHelper.getWritableDatabase();
		readableDatabase = databaseHelper.getReadableDatabase();
	}

	/**
	 * tao mot ban ghi moi trong bang
	 * 
	 * @param article
	 * @return
	 */
	public long createRecord(Article article) {
		ContentValues values = new ContentValues();
		values.put(allColumns[1], article.getServerId());
		values.put(allColumns[2], article.getTitle());
		values.put(allColumns[3], article.getCreatedTime());
		values.put(allColumns[4], article.getLink());
		values.put(allColumns[5], article.getThumbLink());
		long insertId = writableDatabase.insert(Constants.ARTICLE_TABLE, null,
				values);
		article.setId(insertId);
		return insertId;
	}

	/*
	 * de-active 1 article
	 */
	public void deleteArticle(long id) {
		ContentValues values = new ContentValues();
		values.put(allColumns[5], 0);
		writableDatabase.update(Constants.ARTICLE_TABLE, values, allColumns[0]
				+ " = " + id, null);
	}

	/**
	 * map cac gia tri lay duoc tu trong bang vao doi tuong Constants.WORD_ID,
	 * Constants.WORD_KEY, Constants.WORD_SEARCH_SUCCEED,
	 * Constants.WORD_DIC_TYPE, Constants.WORD_SEARCH_RESULT
	 * 
	 * @param cursor
	 * @return
	 */
	private Article cursorToObject(Cursor cursor) {
		Article article = new Article();
		int serverId = cursor.getInt(1);
		String title = cursor.getString(2);
		String createdTime = cursor.getString(3);
		String link = cursor.getString(4);
		String thumbLink = cursor.getString(5);
		int isRead = cursor.getInt(6);
		int isactive = cursor.getInt(7);
		article.setId(cursor.getLong(0));
		article.setServerId(serverId);
		article.setTitle(title);
		article.setCreatedTime(Long.valueOf(createdTime));
		article.setLink(link);
		article.setThumbLink(thumbLink);
		article.setIsRead(isRead);
		article.setIsactive(isactive);
		return article;
	}

	public ArrayList<Article> getAllRecords() {
		ArrayList<Article> listWords = new ArrayList<Article>();

		Cursor cursor = readableDatabase.query(Constants.ARTICLE_TABLE,
				allColumns, null, null, null, null, Constants.ARTICLES_ID
						+ " DESC");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Article word = cursorToObject(cursor);
			listWords.add(word);
			Log.i(TAG, "word id = " + word.getId());
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		Collections.sort(listWords, new ArticleComparator());
		return listWords;
	}

	public Article findArticleById(int id) {
		Cursor cursor = readableDatabase.query(Constants.ARTICLE_TABLE,
				allColumns, Constants.ARTICLES_ID + " = " + id, null, null,
				null, null);

		cursor.moveToFirst();
		Article word = null;
		while (!cursor.isAfterLast()) {
			word = cursorToObject(cursor);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return word;
	}

	/**
	 * lay article theo server id
	 * 
	 * @param id
	 * @return true neu da ton tai article co server id tuong ung chua
	 */
	public boolean findArticleByServerId(int id) {
		Cursor cursor = readableDatabase.query(Constants.ARTICLE_TABLE,
				allColumns, Constants.ARTICLES_SERVER_ID + " = " + id, null,
				null, null, null);

		cursor.moveToFirst();
		Article word = null;
		while (!cursor.isAfterLast()) {
			word = cursorToObject(cursor);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		if (word == null)
			return false;
		else
			return true;
	}

	/**
	 * set article is read
	 * 
	 * @param id
	 *            id cua article trong database
	 */
	public void setArticleIsRead(int id) {
		ContentValues cv = new ContentValues();
		cv.put(Constants.ARTICLES_ISREAD, "1");
		writableDatabase.update(Constants.ARTICLE_TABLE, cv,
				Constants.ARTICLES_ID + "=" + id, null);
	}
}
